/**
 * Created by denh on 16/12/14.
 */
(function(win,doc){
    function change(){
        doc.documentElement.style.fontSize=doc.documentElement.clientWidth/375*23.4375+'px';
    }
    change();
    win.addEventListener('resize',change,false);
})(window,document);

var token = '<?=$this->token?>';
var videoId = common.getParam('id');
var page = 1;
common.serverUrl = '<?=BACK_INTERFACE_BASE_URL?>';

var appContent = new Vue({
    el: '#video',
    data: {
        video_html : ""
    }
});

var appTitle = new Vue({
    el: '#title',
    data: {
        title: ""
    }
});

var appRemark = new Vue({
    el: '#remark',
    data: {
        remarks: []
    },
    methods : {
        reply : function(re) {
            appFooter.message += "@"+re.accountName+" ";
            appFooter.remarkId = re.id;
        }
    }
});

var appFooter = new Vue({
    el: '#footer',
    data: {
        message : "",
        remarkId : ""
    },
    methods : {
        send : function() {
            if (this.message.length == 0) {
                return true;
            }
            common.net('CardController_videoRemark', token, videoId, this.message, this.remarkId, function(result){
                console.log(result);
                if (result.code == 0) {
                    appRemark.remarks.unshift(result.result);
                    appFooter.message = '';
                    $("#remark").scrollTop(0);
                }
            });
        }
    }
});

getData();

$("#remark").scroll(function(){
    if ($(window).height() + $("#remark").scrollTop() == $("#remark").height()) {
        if (page > 0) {
            page++;
            $("#waiting").show();
            getRemarkList(page);
        }
    }
});

/**
 * 获取数据
 */
function getData() {
    common.net('CardController_videoDetail', token, videoId, function(result){
        if(result.code != 0) {
            common.showMsgBox(result.msg);
        } else {
            console.log(result);
            apply(result);
        }

    });
    getRemarkList(page);

}

function getRemarkList(page) {
    common.net('CardController_videoRemarkList', token, videoId, page, function(result){
        console.log(result);
        $("#waiting").hide();
        appRemark.remarks = appRemark.remarks.concat(result.result);
        if (result.result.length < 10) {
            page = 0;
        }
    });
}

/**
 * 数据渲染
 * @param result
 */
function apply(result) {

    share_title = result.result.title;
    share_desc = result.result.title;
    share_imgUrl = result.result.thumbnail;
    share(share_title, share_desc, share_imgUrl);

    video_html = '<video id="media" controls="controls" poster="'+result.result.thumbnail+'" preload="none" x-webkit-airplay="true" webkit-playsinline="true" >'+
    '<source src="'+result.result.video+'" type="video/mp4" x-webkit-airplay="true" webkit-playsinline="true">'+
    '您的浏览器不支持video标签'+
    '</video>';

    appTitle.title = result.result.title;
    appContent.video_html = video_html;
    Vue.nextTick(function(){
        $(".leavemsg").height($(window).height()-$("#video").height()-$("#footer").height()-$(".title").height());
    });
}

/**
 * 微信分享
 * @param share_title
 * @param share_desc
 * @param share_imgUrl
 */
function share(share_title, share_desc, share_imgUrl) {
    wx.config({
        debug: false,
        appId: '<?php echo $this->signPackage["appId"];?>',
        timestamp: '<?php echo $this->signPackage["timestamp"];?>',
        nonceStr: '<?php echo $this->signPackage["nonceStr"];?>',
        signature: '<?php echo $this->signPackage["signature"];?>',
        jsApiList: [
            'onMenuShareAppMessage',
            'onMenuShareTimeline',
            'onMenuShareQQ',
            'onMenuShareWeibo',
            'onMenuShareQZone'
        ]
    });
    links = '';
    weixinShare(share_title, share_desc, links, share_imgUrl);
}